package com.bytecodezz.app.framework.base.general

import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity

/**
 * @author: bytecodezz
 * @version: 1.0
 * @time: 2021/7/5 16:52
 * @package: com.bytecodezz.app.framework.base
 *
 * @description: Activity基类，不建议直接基础，如果直接继承，里面过多的抽象方法需要实现
 *  主要提供的功能
 *  1、默认设置屏幕方向 @see getScreenOrientation()
 *  2、提供2中方式设置布局，按先后顺序：generateContentView返回Int、generateContentView返回View
 *  3、
 *  4、
 */
abstract class BaseActivity : AppCompatActivity(), IBaseActivity {

    override fun onCreate(savedInstanceState: Bundle?) {
        onActionPreSuper()
        super.onCreate(savedInstanceState)
        setScreenOrientation(defaultOrientation())
        onActionPreView()
        setRootContentView(savedInstanceState)
        onActionPostView()
        onActionStart(savedInstanceState)
    }

    private fun setRootContentView(savedInstanceState: Bundle?) {
        generateContentView()?.let { setContentView(it) }
            ?: setContentView(generateContentView(savedInstanceState))
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        //恢复状态
        restoreInstanceState(savedInstanceState)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        //创建时不同启动模式导致生命周期不创建 而是调用该方法
        newIntent(intent)
    }

    override fun newIntent(intent: Intent?) {
        //不强制子类重写方法，供子类选择性重写
    }

    override fun saveInstanceState(outState: Bundle?) {
        //不强制子类重写方法，供子类选择性重写
    }

    override fun restoreInstanceState(outState: Bundle?) {
        //不强制子类重写方法，供子类选择性重写
    }

    override fun onActionPreSuper() {
        //不强制子类重写方法，供子类选择性重写
    }

    override fun onActionPreView() {
        //不强制子类重写方法，供子类选择性重写
    }

    override fun onActionPostView() {
        //不强制子类重写方法，供子类选择性重写
    }

    override fun onKeyBack(): Boolean {
        finish()
        return true
    }

    override fun onKeyHome(): Boolean {
        //不强制子类重写方法，供子类选择性重写
        return true
    }

    override fun onKeyMenu(): Boolean {
        //不强制子类重写方法，供子类选择性重写
        return true
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        when (keyCode) {
            KeyEvent.KEYCODE_BACK -> return onKeyBack()
            KeyEvent.KEYCODE_HOME -> return onKeyHome()
            KeyEvent.KEYCODE_MENU -> return onKeyMenu()
        }
        return super.onKeyDown(keyCode, event)
    }


    /**
     * @author     : bytecodezz
     * @version    : version 1.0
     * @description: 屏幕方向配置，根据返回值进行方向设置，常用配置参数如下
     * @param null
     * @return Int
     *  SCREEN_ORIENTATION_PORTRAIT 强制竖屏 (默认选项)
     *  SCREEN_ORIENTATION_LANDSCAPE 强制横屏
     *  SCREEN_ORIENTATION_USER 自动旋转(受用户方向锁定功能开关限制)
     *  SCREEN_ORIENTATION_SENSOR 自动旋转(不受用户方向锁定开关限制)
     *
     */
    override fun defaultOrientation(): Int {
        return ActivityInfo.SCREEN_ORIENTATION_SENSOR
    }

    override fun setScreenOrientation(orientation: Int) {
        requestedOrientation = orientation
    }
}